package com.zqwh.storemonitor.model;

import android.content.Context;

import com.zqwh.storemonitor.Config;
import com.zqwh.storemonitor.api.WatcherApi;
import com.zqwh.storemonitor.domain.Shop;
import com.zqwh.storemonitor.domain.ShopBell;
import com.zqwh.storemonitor.domain.ShopWatcher;
import com.zqwh.yj.sdk.utils.httpRequest.HttpResultFunc2;
import com.zqwh.yj.sdk.utils.httpRequest.HttpResultFunc4;
import com.zqwh.yj.sdk.utils.httpRequest.RetrofitSingleton;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.ProgressSubscriber;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class WatcherModel {
    private Context context;
    private WatcherApi watcherApi;

    public WatcherModel(Context context) {
        this.context = context;
        this.watcherApi = RetrofitSingleton.getInstance(Config.BASE_URL).create(WatcherApi.class);
    }

    public void setGeTuiCid(Integer shopWatcherId, String cid, NetCallback<Object> callback){
        watcherApi.setGeTuiCid(shopWatcherId, cid)
                .map(new HttpResultFunc4<>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, true));
    }

    public void getAccessToken(NetCallback<String> callback){
        watcherApi.getAccessToken()
                .map(new HttpResultFunc4<String>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, true));
    }

    public void login(String account, String password, NetCallback<ShopWatcher> callback){
        watcherApi.login(account, password)
                .map(new HttpResultFunc4<ShopWatcher>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, true));
    }

    public void getShopList(int shopWatcherId, NetCallback<List<Shop>> callback){
        watcherApi.getShopList(shopWatcherId)
                .map(new HttpResultFunc4<List<Shop>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, true));
    }

    public void getShopBellList(int shopWatcherId, NetCallback<List<ShopBell>> callback){
        watcherApi.getShopBellList(shopWatcherId)
                .map(new HttpResultFunc4<List<ShopBell>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, true));
    }

    public void setOutAllow(String devSn, boolean isOutAllow, NetCallback<ShopBell> callback){
        watcherApi.setOutAllow(devSn, isOutAllow)
                .map(new HttpResultFunc4<ShopBell>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, true));
    }

    public void getOutAllow(String devSn, NetCallback<Boolean> callback){
        watcherApi.getOutAllow(devSn)
                .map(new HttpResultFunc4<Boolean>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, true));
    }
}
